﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using BitcoinCommon;
using BitcoinExchange.Data;


namespace BitcoinExchange.Contract
{
    public class BTCeContract
    {
        MainInfo _info;
        public DataOpenOrder OpenOrderTable;

        public BTCeContract(MainInfo info)
        {
            _info = info;
            OpenOrderTable = new DataOpenOrder();
        }

        public void RetrivedOpenOrder()
        {
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            Dictionary<string, string> dicURLParameter = _info.GetBTCURLParameterAccessKey();
            dicURLParameter.Add("method", "ActiveOrders");
            dicURLParameter.Add("pair", "btc_usd");


            //접속 주소를 입력한다.
            strURL = _info.GetBTCeURL() + StringURL.URL_BTCE_ORDER_CANCEL;
            strParameters = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameters, _info.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return;

            ConvertToOpenOrder(strData);

        }

        public List<string> OrderIDList()
        {
            return OpenOrderTable.OrderIDList();
        }

        public Dictionary<string, string> OrderInfo(string strOrderID)
        {
            return OpenOrderTable.OrderInfo(strOrderID);
        }

        private void ConvertToOpenOrder(string strData)
        {
            DataOpenOrder objTable = new DataOpenOrder();

            string strOrderID;
            string strOrderType;
            double dbBTCPrice;
            double dbBTCAmount;

            if (strData.Length == 0)
                return;

            JObject objData = JObject.Parse(strData);
            objData = (JObject)objData["return"];

            objTable.Clear();

            if (objData == null)
                return;

            foreach (JProperty oObjItem in objData.Properties())
            {
                try
                {
                    strOrderID = oObjItem.Name;

                    JToken oSubItem = objData[strOrderID];
                    strOrderType = oSubItem["type"].ToString();
                    dbBTCPrice = Double.Parse(oSubItem["rate"].ToString());
                    dbBTCAmount = Double.Parse(oSubItem["amount"].ToString());

                    objTable.AddRow(strOrderID, strOrderType, dbBTCPrice, dbBTCAmount, 0.0);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }

            if (objTable.RowCount() == 0)
                return;

            OpenOrderTable = objTable;
        }
    }
}
